package hk.org.ha.cdars.job;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import hk.org.ha.cdars.datacontext.BisDataContext.G1CriterialNode;
import hk.org.ha.cdars.datacontext.BisDataContext.G1RefG2CheckResult;
import hk.org.ha.cdars.datacontext.BisDataContext.G2CriterialNode;
import lava.rt.common.UtilCommon;
import lava.rt.linq.CommandExecuteExecption;

@Service
public class G1RefG2CheckJob extends CheckJob<G1RefG2CheckResult> {

	
	
	@Override
	public List<G1RefG2CheckResult> check() throws Exception {
		List<G1RefG2CheckResult> rets=new ArrayList<>();
		// TODO Auto-generated method stub
		 
		List<G2CriterialNode> g2nodes=dataContext.G2_CRITERIAL_NODE.selectList("",CR.criteriaType.asc );
		
		Map<String, G2CriterialNode> g2nodeMap=UtilCommon.toMap(g2nodes, (v)->{ return v.getPath(); });
		List<String> diffMsgs=new ArrayList<>();
		dataContext.G1_CRITERIAL_NODE.selectCursoring((index,g1node)->{
			diffMsgs.clear();
			G1RefG2CheckResult ret=new G1RefG2CheckResult();
			ret.setG1Path(g1node.getPath());
			G2CriterialNode g2Node=g2nodeMap.get(g1node.getPath());
			if(g2Node==null) {
			   ret.setFlag(Flag.不存在.code);	
			}else if(compare(g1node, g2Node, diffMsgs)>0) {
				ret.setFlag(Flag.差異.code);
				ret.setRemark(String.join(";", diffMsgs));
			
			}
			else {
				ret.setFlag(Flag.相同.code);	
			}
			ret.setTreeType(g1node.getTreeType());
			ret.setNodeType(getNodeType(g1node));
			ret.setJobNo(getJobNo());
			
			rets.add(ret);
			return 0;
		}, CR.remark.notEq("warning"), CR.criteriaType.asc);
		
		return rets;
	}

}
